/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ucuenca.edu.optii.ecu911.gui;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import ucuenca.edu.optii.ecu911.negocio.EntidadCooperativa;
import ucuenca.edu.optii.ecu911.negocio.Entidad_TipIncidente;
import ucuenca.edu.optii.ecu911.negocio.TipoIncidente;
import ucuenca.edu.optii.ecu911.negocio.VectoresObj;
import ucuenca.edu.optii.ecu911.negocio.Visitor;
import ucuenca.edu.optii.ecu911.negocio.excepciones.ValidacionCamposTextoExcepcion;
import ucuenca.edu.optii.ecu911.negocio.excepciones.ValidacionGrabacionDatos;

/**
 *
 * @author May
 */
public class GestionTipoIncident_Entidades extends javax.swing.JPanel {

    String entId;
    VectoresObj vect = new VectoresObj();
    EntidadCooperativa unE = new EntidadCooperativa() {

        @Override
        public String aceptar(Visitor visitor) {
            return "";
        }
    };
    Entidad_TipIncidente MuchosM = new Entidad_TipIncidente();
    TipoIncidente mitipo=new TipoIncidente();

    /**
     * Creates new form GestionTipoIncident_Entidades
     */
    public GestionTipoIncident_Entidades() {
        initComponents();
    }

    public GestionTipoIncident_Entidades(String mientidadId) {
        initComponents();
        entId = mientidadId;
        inicia();
        actualizaTablaM_M(Integer.parseInt(entId));
    }

    public void inicia() {
        jLabel1.setText(entId);
        int idEnt = Integer.valueOf(jLabel1.getText());
        unE.setId(idEnt);
        String entTipo = unE.buscarEntidadHijo();
        if (entTipo != null) {
            txtentidad.setText(entTipo);
        }
        actualizaComboTipoIncidentes();
    }

    public void actualizaTablaM_M(int idEntidad) {
        vect.listarTiposIncidentesSegunEntidades(idEntidad);
        ArrayList listado;
        listado = vect.getEntidades_tiposInc();
        DefaultTableModel temp = (DefaultTableModel) tabla.getModel();
        for (int i = tabla.getRowCount() - 1; i >= 0; i--) {
            temp.removeRow(i);
        }
        for (int i = 0; i < listado.size(); i++) {
            Entidad_TipIncidente otrop ;
            otrop = (Entidad_TipIncidente) listado.get(i);
            mitipo.setId(otrop.getMiTipoIn().getId());
            mitipo.buscarDescripcion();  
            Object nuevo[] = {otrop.getMientidad().getId(), mitipo.getDescripcion()}; //esto es por las tres columnas aunque puede variar
            temp.addRow(nuevo);
        }
        tabla.setModel(temp);
    }

    public void actualizaComboTipoIncidentes() {
        List tipositems;
        vect.listarTipoIncidentes();
        tipositems = vect.getTiposInc();
        comboTipoInci.removeAllItems();
        comboTipoInci.addItem("");
        for (int i = 0; i < tipositems.size(); i++) {
            TipoIncidente otroTipo;
            otroTipo = (TipoIncidente) tipositems.get(i);
            comboTipoInci.addItem(otroTipo.getId() + ", " + otroTipo.getDescripcion());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        comboTipoInci = new javax.swing.JComboBox();
        ponTipoInc = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtentidad = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Entidad", "Tipo_Incidente Permitido"
            }
        ));
        jScrollPane1.setViewportView(tabla);

        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jLabel2.setText("Tipos Permitidos");

        comboTipoInci.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        comboTipoInci.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                comboTipoInciMouseClicked(evt);
            }
        });

        ponTipoInc.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));

        jButton1.setText("add");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jLabel4.setText("Entidad Actual:");

        jLabel5.setText("Nombre:");

        jLabel6.setFont(new java.awt.Font("Tempus Sans ITC", 0, 14)); // NOI18N
        jLabel6.setText("Tipo de Incidentes que Atenderan Entidades Cooperativas ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(84, 84, 84)
                                .addComponent(jLabel2))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtentidad, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(ponTipoInc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1))
                            .addComponent(comboTipoInci, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(38, 38, 38))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(comboTipoInci)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(txtentidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(ponTipoInc, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(33, 33, 33)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void comboTipoInciMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_comboTipoInciMouseClicked
        StringTokenizer tokens = new StringTokenizer((String) comboTipoInci.getSelectedItem(), ",");
        try {
            ponTipoInc.setText(tokens.nextToken());
        } catch (NoSuchElementException e) {
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_comboTipoInciMouseClicked

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if (jLabel1.getText().equals("") || ponTipoInc.getText().equals("")) {
           throw new ValidacionCamposTextoExcepcion( "Debe llenar los campos de texto");
        } else {
            int miEnt = Integer.valueOf(jLabel1.getText());
            int miTipInc = Integer.valueOf(ponTipoInc.getText());
            boolean r = MuchosM.grabar(miTipInc, miEnt);
            if (r == true) {
                JOptionPane.showMessageDialog(null, "Datos Grabados Satisfactoriamente...");
            } else if (r == false) {
               throw new ValidacionGrabacionDatos( "Error al Grabar datos");
            }
            actualizaTablaM_M(miEnt);
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboTipoInci;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel ponTipoInc;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txtentidad;
    // End of variables declaration//GEN-END:variables
}
